package com.googlecode.mylyn.ui.wizard;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckForNull;

/**
 * Holds values to all the predefined canned queries by the Google API.
 *
 * @see http://code.google.com/p/support/wiki/IssueTrackerAPIReference
 */
enum CannedQueryIdentifier {

    ALL("all", false, "All issues"),
    OPEN("open", false, "Open issues"),
    OWNED("owned", true, "Open and owned by me"),
    REPORTED("reported", true, "Open and reported by me"),
    STARRED("starred", true, "Open and starred by me"),
    NEW("new", false, "New issues"),
    TO_VERIFY("to-verify", false, "Issues to verify");

    private final String parameter;
    private final boolean needsAuthentication;
    private final String description;

    private CannedQueryIdentifier(String parameter, boolean needsAuthentication, String description) {
        this.parameter = parameter;
        this.needsAuthentication = needsAuthentication;
        this.description = description;
    }

    /**
     * Returns the actual parameter value that has to be sent to the server.
     *
     * @return the parameter value
     */
    String getParameter() {
        return parameter;
    }

    /**
     * Returns a boolean if the query needs an authenticated user or not.
     *
     * @return <code>true</code> if authentication is needed, <code>false</code> aotherwise
     */
    public boolean isNeedsAuthentication() {
        return needsAuthentication;
    }

    /**
     * Returns a human understandable string that describes what the query
     * identified by this value does. Only used for display purposes.
     *
     * @return an explanation string
     */
    String getDescription() {
        return description;
    }

    /**
     * Given the query parameter value this method returns the matching
     * {@link CannedQueryIdentifier}.
     *
     * @see #getParameter()
     * @param parameter the query parameter
     * @return the matching {@link CannedQueryIdentifier}, {@literal null} if
     *  none could be found
     */
    @CheckForNull
    static CannedQueryIdentifier fromParameter(String parameter) {
        for (CannedQueryIdentifier identifier : values()) {
            if (identifier.getParameter().equals(parameter)) {
                return identifier;
            }
        }
        return null;
    }

    /**
     * Returns all queries with don't need authentication.
     *
     * @see #values()
     */
    public static CannedQueryIdentifier[] anonymousValues() {
        List<CannedQueryIdentifier> anonymousValues = new ArrayList<CannedQueryIdentifier>();
        for (CannedQueryIdentifier cannedQueryIdentifier : values()) {
            if (!cannedQueryIdentifier.isNeedsAuthentication()) {
                anonymousValues.add(cannedQueryIdentifier);
            }
        }
        return anonymousValues.toArray(new CannedQueryIdentifier[anonymousValues.size()]);
    }

}
